﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ThreeByte.ActiveDeck.Config;
using System.Windows.Forms.Integration;
using ThreeByte.ActiveDeck.AddIn.Controls;
using ThreeByte.ActiveDeck.AddIn.Controls.Wizard;

namespace ThreeByte.ActiveDeck.Controls
{
    public partial class WizardForm : Form
    {
        public WizardForm(WizardMainFrame frame ) {
            InitializeComponent();
            
            this.Load += new EventHandler(WizardForm_Load);
            frame.Close += new EventHandler<DialogResultEventArgs>(frame_Close);
            frame.Move += new EventHandler<MoveEventArgs>(frame_Move);

            //Add WPF Host Frame
            ElementHost elementHost = new ElementHost();
            elementHost.Dock = DockStyle.Fill;
            elementHost.Location = new Point(0, 0);
            elementHost.Name = "elementHost";
            elementHost.Child = frame;
            elementHost.AutoSize = true;
            elementHost.BackColorTransparent = true;
            elementHost.BackColor = Color.Transparent;
            this.Controls.Add(elementHost);
            

            //this.ClientSize = new System.Drawing.Size((int)(frame.MinWidth), (int)(frame.MinHeight));

            //this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
            //this.BackColor = Color.Transparent;

            //string titleOverride = userControl.Tag as string;
            //if(!(string.IsNullOrEmpty(titleOverride))) {
            //    this.Text = titleOverride;
            //}
            
        }

        void frame_Move(object sender, MoveEventArgs e) {
            this.Top = this.Top + (int)(Math.Round(e.Offset.Y));
            this.Left = this.Left + (int)(Math.Round(e.Offset.X));
        }

        void WizardForm_Load(object sender, EventArgs e) {
            this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
            this.BackColor = Color.Transparent;
        }

        protected override CreateParams CreateParams {
            get {
                CreateParams cp = base.CreateParams;
                cp.ExStyle |= 0x00000020; //WS_EX_TRANSPARENT
                return cp;
            }
        }
        void frame_Close(object sender, DialogResultEventArgs e) {
            this.DialogResult = e.DialogResult;
            this.Close();
        }

        private void HostForm_FormClosing(object sender, FormClosingEventArgs e) {
            
        }

        #region Aspect Ratio Resizing

        //double so division keeps decimal points
        const double widthRatio = 702;
        const double heightRatio = 500;
        const double wOffset = 24;
        const double hOffset = 0;

        const int WM_SIZING = 0x214;
        const int WMSZ_LEFT = 1;
        const int WMSZ_RIGHT = 2;
        const int WMSZ_TOP = 3;
        const int WMSZ_BOTTOM = 6;

        public struct RECT
        {
            public int Left;
            public int Top;
            public int Right;
            public int Bottom;
        }

        //Maintain aspect ratio of the window
        protected override void WndProc(ref Message m) {
            if(m.Msg == WM_SIZING) {
                RECT rc = (RECT)System.Runtime.InteropServices.Marshal.PtrToStructure(m.LParam, typeof(RECT));
                int res = m.WParam.ToInt32();
                if(res == WMSZ_LEFT || res == WMSZ_RIGHT) {
                    //Left or right resize -> adjust height (bottom)
                    rc.Bottom = rc.Top + (int)((heightRatio * (this.Width - wOffset) / widthRatio) + hOffset);
                } else if(res == WMSZ_TOP || res == WMSZ_BOTTOM) {
                    //Up or down resize -> adjust width (right)
                    rc.Right = rc.Left + (int)((widthRatio * (this.Height - hOffset) / heightRatio) + wOffset);
                } else if(res == WMSZ_RIGHT + WMSZ_BOTTOM) {
                    //Lower-right corner resize -> adjust height (could have been width)
                    rc.Bottom = rc.Top + (int)((heightRatio * (this.Width - wOffset) / widthRatio) + hOffset);
                } else if(res == WMSZ_LEFT + WMSZ_TOP) {
                    //Upper-left corner -> adjust width (could have been height)
                    rc.Left = rc.Right - (int)((widthRatio * (this.Height - hOffset) / heightRatio) - wOffset);
                }
                System.Runtime.InteropServices.Marshal.StructureToPtr(rc, m.LParam, true);
            }

            base.WndProc(ref m);
        }

        #endregion Aspect Ratio Resizing
    }
}
